package com.tbit.uqbike.service.business.impl;

import com.github.pagehelper.PageHelper;
import com.tbit.uqbike.object.pojo.AccountUser;
import com.tbit.uqbike.object.pojo.MachineCleanLog;
import com.tbit.common.entity.pojo.vo.DateResult;
import com.tbit.uqbike.object.pojo.statistics.ExportMachineCleanLog;
import com.tbit.uqbike.object.pojo.statistics.ExportPowerLog;
import com.tbit.uqbike.service.business.MachineCleanLogService;
import com.tbit.uqbike.webmanager.dao.core.AccountUserDao;
import com.tbit.uqbike.webmanager.dao.log.MachineCleanLogDao;
import com.tbit.utils.DateTimeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 清洁车日志(MachineCleanLog)表服务实现类
 *
 * @author jing
 * @since 2021-01-11 13:48:31
 */
@Service("machineCleanLogService")
public class MachineCleanLogServiceImpl implements MachineCleanLogService {
    @Resource
    private MachineCleanLogDao machineCleanLogDao;
    @Resource
    private AccountUserDao accountUserDao;

    /**
     * 通过ID查询单条数据
     *
     * @param machineCleanLogId 主键
     * @return 实例对象
     */
    @Override
    public MachineCleanLog queryById(Integer machineCleanLogId) {
        return this.machineCleanLogDao.queryById(machineCleanLogId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<MachineCleanLog> queryAllByLimit(int offset, int limit) {
        return this.machineCleanLogDao.queryAllByLimit(offset, limit);
    }

    @Override
    public List<DateResult> statisticsByTime(MachineCleanLog machineCleanLog) {
        List<String> daysStr = DateTimeUtil.findDaysStr(DateTimeUtil.formatDateYMD(machineCleanLog.getStartTime()), DateTimeUtil.formatDateYMD(machineCleanLog.getEndTime()));
        List<DateResult> result = new ArrayList<>();
        daysStr.stream().forEach(day -> {
            machineCleanLog.setStartTime(DateTimeUtil.StringToDateTime(day + " 00:00:00"));
            machineCleanLog.setEndTime(DateTimeUtil.StringToDateTime(day + " 23:59:59"));
            DateResult dateResult = machineCleanLogDao.statisticsByTime(machineCleanLog);
            dateResult.setDate(day);
            result.add(dateResult);
        });
        return result;
    }

    @Override
    public List<MachineCleanLog> queryPage(MachineCleanLog machineCleanLog) {
        PageHelper.startPage(machineCleanLog.getPage().getPageNO(), machineCleanLog.getPage().getRowCount());
        return machineCleanLogDao.queryAll(machineCleanLog);
    }

    @Override
    public List<ExportMachineCleanLog> exportByTime(MachineCleanLog machineCleanLog) {
        return machineCleanLogDao.exportByTime(machineCleanLog);
    }

    @Override
    public List<ExportPowerLog> getById(MachineCleanLog machineCleanLog) {
        PageHelper.startPage(machineCleanLog.getPage().getPageNO(), machineCleanLog.getPage().getRowCount());
        List<ExportPowerLog> results = machineCleanLogDao.getById(machineCleanLog);
        if (results.size() > 0) {
            results.stream().forEach(result -> {
                AccountUser accountUser = accountUserDao.getAccountUserById(result.getAccountUserId());
                if (null != accountUser) {
                    result.setPhone(accountUser.getPhone());
                }
            });
        }

        return results;
    }

    /**
     * 新增数据
     *
     * @param machineCleanLog 实例对象
     * @return 实例对象
     */
    @Override
    public MachineCleanLog insert(MachineCleanLog machineCleanLog) {
        this.machineCleanLogDao.insert(machineCleanLog);
        return machineCleanLog;
    }

    /**
     * 修改数据
     *
     * @param machineCleanLog 实例对象
     * @return 实例对象
     */
    @Override
    public MachineCleanLog update(MachineCleanLog machineCleanLog) {
        this.machineCleanLogDao.update(machineCleanLog);
        return this.queryById(machineCleanLog.getMachineCleanLogId());
    }

    /**
     * 通过主键删除数据
     *
     * @param machineCleanLogId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer machineCleanLogId) {
        return this.machineCleanLogDao.deleteById(machineCleanLogId) > 0;
    }
}